//
//  YMCheckScanViewController.m
//  YMMedical
//
//  Created by y s on 2023/7/14.
//

#import "YMCheckScanViewController.h"
#import "YMMineService.h"
#import "YMCheckInformationModel.h"

@interface YMCheckScanViewController ()

@property (nonatomic, strong) UIView *vBg;

@property (nonatomic, strong) UILabel *lblTips;
@property (nonatomic, strong) UIImageView *imgvScan;

@property (nonatomic, strong) UIButton *btnStart;

@end

@implementation YMCheckScanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

- (void)setUI{
    [self.view addSubview:self.vBg];
    [self.vBg addSubview:self.lblTips];
    [self.vBg addSubview:self.imgvScan];
    
    [self.view addSubview:self.btnStart];
    
    [self.vBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.top.mas_equalTo(0);
    }];
    [self.lblTips mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.top.mas_equalTo([SFitUtil fit:15]);
    }];
    [self.imgvScan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lblTips.mas_bottom).offset([SFitUtil fit:30]);
        make.bottom.mas_equalTo([SFitUtil fit:-40]);
        make.size.mas_equalTo([SFitUtil fitSize:224 height:84]);
        make.centerX.mas_equalTo(0);
    }];
    [self.btnStart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-IPHONE_X_BOTTOM_HEIGHT-[SFitUtil fit:15]);
        make.size.mas_equalTo([SFitUtil fitSize:345 height:54]);
        make.centerX.mas_equalTo(0);
    }];
}


- (void)startClickAction:(id)sender{
    WEAK_SELF;
    [YMBridging gotoScanCodeVC:YES isCard:YES finishBlock:^(NSString * _Nonnull valueStr) {
        //选择
        [weakSelf checkDeviceIsTrue:valueStr];
    }];
//    [self checkDeviceIsTrue:@"10010223081051"];
    
}
//检查是否是有此试剂卡
- (void)checkDeviceIsTrue:(NSString *)snCode{
    WEAK_SELF;
    [SHUD showWait:self.view];
    [YMCheckService requestCheckDetectionCardDataWithIdStr:_checkModel.minorTermId cardCode:snCode success:^(BOOL canUse, NSDictionary * _Nonnull dictData) {
        [SHUD hide:weakSelf.view];
        if (canUse){
            weakSelf.checkModel.minorTermId = [SDataUtil stringOf:dictData[@"tkynuu9ecdit0llv"]];
            weakSelf.checkModel.stickerId = [SDataUtil stringOf:dictData[@"tkynuu9ecdit0llv"]];
            weakSelf.checkModel.waitTime = [SDataUtil stringOf:dictData[@"danmoe2hn6ie3yqu"]];
            weakSelf.checkModel.outTime = [SDataUtil stringOf:dictData[@"fbmfxcaqgsgscrsw"]];
            weakSelf.checkModel.measLowEnd = [SDataUtil stringOf:dictData[@"v6qpfu0mxss3cw1t"]];
            weakSelf.checkModel.measHighEnd = [SDataUtil stringOf:dictData[@"wn3d1qkftirpidef"]];
            weakSelf.checkModel.measUnit = [SDataUtil stringOf:dictData[@"n7cplrqzmakiyazi"]];
            weakSelf.checkModel.volumeUnit = [SDataUtil stringOf:dictData[@"3ntlcnyxsmlzs1w0"]];
            
            NSArray *arrMinorTerm = [SDataUtil arrayOf:dictData[@"lcq6twxkuyytwpkc"]];
            
            if (arrMinorTerm != nil && arrMinorTerm.count > 0){
                NSDictionary *dictMinorTerm = [SDataUtil dictOf:arrMinorTerm.firstObject];
                weakSelf.checkModel.minorTermId = [SDataUtil stringOf:dictMinorTerm[@"id"] valid:@""];
                weakSelf.checkModel.minorTermName = [SDataUtil stringOf:dictMinorTerm[@"value"] valid:@""];
            }
            weakSelf.checkModel.minorTermEnglishName = [SDataUtil stringOf:dictData[@"d5zyxqmhtbzruvz6"] valid:@""];
            if (weakSelf.finishBlock){
                weakSelf.finishBlock(weakSelf.checkModel);
            }
        }else{
            [SHUD showInfo:KLanguage(@"no_find_card_txt")];
        }
    } failure:^(NSString * _Nonnull message) {
        [SHUD hide:weakSelf.view];
        [SHUD showInfo:message];
    }];
}

#pragma mark -- setter and getter
- (UIView *)vBg{
    if (_vBg == nil){
        _vBg = [[UIView alloc] init];
        _vBg.backgroundColor = STheme.D9;
        _vBg.clipsToBounds = YES;
        _vBg.layer.cornerRadius = [SFitUtil fit:8];
    }
    return _vBg;
}
- (UILabel *)lblTips{
    if (_lblTips == nil){
        _lblTips = [SThemeUtil boldLabel:@"" color:STheme.H_D1 fontSize:16];
        _lblTips.numberOfLines = 0;
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = [SFitUtil fit:8];
        NSAttributedString *attr = [[NSAttributedString alloc] initWithString:KLanguage(@"reagent_card_hint") attributes:@{NSParagraphStyleAttributeName: style}];
        _lblTips.attributedText = attr;
        _lblTips.textAlignment = NSTextAlignmentCenter;
    }
    return _lblTips;
}
- (UIImageView *)imgvScan{
    if (_imgvScan == nil){
        _imgvScan = [[UIImageView alloc] init];
        _imgvScan.image = [UIImage imageNamed:@"check_scan_step1_device_icon"];
    }
    return _imgvScan;
}
- (UIButton *)btnStart{
    if (_btnStart == nil){
        _btnStart = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnStart setTitle:[NSString stringWithFormat:@"  %@",KLanguage(@"start_scan_txt")] forState:UIControlStateNormal];
        [_btnStart setTitleColor:STheme.D9 forState:UIControlStateNormal];
        _btnStart.titleLabel.font = [SFitUtil fitBoldFont:16];
        [_btnStart setImage:[UIImage imageNamed:@"check_step1_button_scan_icon"] forState:UIControlStateNormal];
        [_btnStart addTarget:self action:@selector(startClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnStart.backgroundColor = STheme.D12;
        _btnStart.layer.cornerRadius = [SFitUtil fit:10];
        _btnStart.clipsToBounds = YES;
    }
    return _btnStart;
}


@end
